<template>
    <basic-container>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-setting"></i> 账单管理</el-breadcrumb-item>
                <el-breadcrumb-item><i class="el-icon-date"></i> 审核</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <avue-form ref="form" v-model="form" :option="reviewOption">
            <template slot-scope="scope" slot="menuForm">
                <el-button type="success" @click="handleSuccess">通 过</el-button>
                <el-button type="warning" @click="handleFailed">不通过</el-button>
                <el-button @click="goBack">返 回</el-button>
            </template>
        </avue-form>
        <avue-detail ref="detail_form" v-model="detailForm" :option="option">
        </avue-detail>
        <avue-crud
                ref="crud" :option="orderDetailOption"
                :data="orderDetailList"
        ></avue-crud>
    </basic-container>
</template>

<script>
    import {fetchInfo, reviewFailed, reviewSuccess} from '../../api/bill/receivable';
    import {formOption} from '../../const/crud/bill/form_option/receivable';
    import {formOption as reviewOption} from '../../const/crud/bill/form_option/receivable_review';
    import {formOption as orderDetailOption} from '../../const/crud/bill/form_option/order_detail';
    import {goBack} from "../../util/util";

    export default {
        data() {
            return {
                form: {},
                detailForm: {},
                option: formOption,
                reviewOption: reviewOption,
                orderDetailOption:orderDetailOption,
                orderDetailList:[]
            }
        },
        computed: {},
        created() {
        },
        beforeRouteEnter(to, from, next) {
            next(vm => {
                vm.form.billSn = vm.$route.query.res.billSn;
                vm.queryData(vm.$route.query.res.billSn);
            });
        },
        methods: {
            queryData(billSn) {
                fetchInfo({billSn: billSn}).then(response => {
                    this.detailForm = Object.assign({}, response.data.data, response.data.data.orderInfo);
                    this.orderDetailList = this.detailForm.detailList;
                }).catch(() => {
                    this.goBack();
                });
            },
            handleSuccess() {
                let params = {
                    billSn: this.form.billSn,
                    remarks: this.form.remarks,
                };
                reviewSuccess(Object.assign({}, params)).then(response => {
                    if (response.data.code === 0) {
                        this.$message.success('审核通过, 操作成功')
                        this.goBack();
                    } else {
                        this.$message.success(response.data.msg)
                    }
                })
            },
            handleFailed() {
                let params = {
                    billSn: this.form.billSn,
                    remarks: this.form.remarks,
                };
                reviewFailed(Object.assign({}, params)).then(response => {
                    if (response.data.code === 0) {
                        this.$message.success('审核不通过, 操作成功')
                        this.goBack();
                    } else {
                        this.$message.success(response.data.msg)
                    }
                })
            },
            goBack(time = 1000) {
                goBack(this, time);
            }
        }
    }
</script>

<style scoped lang="scss">
    .crumbs {
        margin: 20px 10px;
    }
</style>


